package com.v1.video.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.domain.CommentEntity;
import com.v1.video.domain.CommentEntity.CommentInfo;
import com.v1.video.domain.LoginInfo;
import com.v1.video.domain.RequestData;
import com.v1.video.domain.SetHead;
import com.v1.video.httpApi.V1VideoHttpApi;
import com.v1.video.httpmanager.RequestManager;
import com.v1.video.httpmanager.RequestManager.OnResponseListener;
import com.v1.video.util.Logger;
import com.v1.video.view.DanmuView;
import com.v1.video.view.DanmuViewBaseAdapter;
import com.v1.video.widgets.periscope.PeriscopeLayout;

import java.util.ArrayList;
import java.util.List;

public class DanmuAdapter extends DanmuViewBaseAdapter {
	private Context mContext;
	private List<CommentInfo> commentinfos = new ArrayList<CommentInfo>();
	private DanmuView mDanmuView;

	public DanmuAdapter(Context context, DanmuView danmuView) {
		mContext = context;
		mDanmuView = danmuView;
	}

	public void addDataList(List<CommentInfo> infos) {
		if (commentinfos != null) {
			commentinfos.addAll(infos);
			mDanmuView.notifyDatasetChange();
		}
	}

	public void addSingleData(CommentInfo info) {
		if (commentinfos != null) {
			commentinfos.add(info);
			mDanmuView.notifyDatasetChange();
		}
	}

	public void clearDataList() {
		if (commentinfos != null) {
			commentinfos.clear();
			mDanmuView.notifyDatasetChange();
		}
	}

	@Override
	public int getCount() {
		return commentinfos.size();
	}

	@Override
	public View getView(final int position, View convertView) {
		View view = View.inflate(mContext, R.layout.danmu_item, null);
		ImageView usericon = (ImageView) view
				.findViewById(R.id.danmu_item_usericon);
		TextView content = (TextView) view
				.findViewById(R.id.danmu_item_content);
		LinearLayout layout = (LinearLayout) view.findViewById(R.id.danmu_item_content_layout);
		final PeriscopeLayout periscopeLayout = (PeriscopeLayout) view.findViewById(R.id.danmu_item_periscope);
		final TextView praise = (TextView) view.findViewById(R.id.danmu_item_praise);

		final CommentInfo commentInfo = commentinfos.get(position);
		new SetHead().setHead(commentInfo.getAvatar(), "", usericon);
		content.setText(commentInfo.getContent() + " " + position);
		praise.setText(commentInfo.getSupport_num());
		layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				periscopeLayout.addSequeFavor();
				requestCommentPraise(commentInfo, praise);
			}
		});
		return view;
	}

	private void requestCommentPraise(final CommentEntity.CommentInfo commentInfo, final TextView tvPraise) {
		String uid = "0";
		if (LoginInfo.getInstance().isLogin()) {
			uid = LoginInfo.getInstance().getUserId();
		}
		String url = String.format(V1VideoHttpApi.COMMENT_PRAISE(),
				Constant.COMMENT_VIDEO_TYPE, commentInfo.getObject_id(), uid,
				commentInfo.getId(), Constant.DEVICEID);
		Logger.i("requestCommentPraise", "点赞url=" + url);
		RequestManager.getInstance().getRequest(mContext, url,
				RequestData.class, new OnResponseListener() {

					@Override
					public void onSuccess(Object object, int code,
							String message, String keyId) {
						if (object != null) {
							RequestData entity = (RequestData) object;
							if (entity != null
									&& entity.getBody() != null
									&& !TextUtils.isEmpty(entity.getBody()
											.getResultCode())) {
								if ("1".equals(entity.getBody().getResultCode())) {
									int num = 0;
									if (!TextUtils.isEmpty(commentInfo
											.getSupport_num())) {
										num = Integer.parseInt(commentInfo
												.getSupport_num()) + 1;
									}
									commentInfo.setSupport_num(num + "");
									tvPraise.setText(num + "");
								} else {
									if (!TextUtils.isEmpty(entity.getBody()
											.getMessage())) {
										Toast.makeText(mContext,
												entity.getBody().getMessage(),
												Toast.LENGTH_SHORT).show();
									}
								}
							}
						}
					}

					@Override
					public void onFailure(int code, String message) {

					}
				});
	}
}
